﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.Common.Domains.Manages;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week01.ManageCommands.Clientses
{
    //客户删除
    public class ClientsDeleteHandler : IRequestHandler<ClientsDeleteCommend, Result>
    {
        private readonly IBaseRepository<Clients> Clients;

        public ClientsDeleteHandler(IBaseRepository<Clients> Clients)
        {
            this.Clients = Clients;
        }

        public async Task<Result> Handle(ClientsDeleteCommend request, CancellationToken cancellationToken)
        {
            var str = new Result() { Code = (int)CommonErrorCode.Delete };

            try
            {
                var list = await Clients.GetAll().Where(x => x.CustomerId == request.CustomerId).FirstAsync();
                if (list != null)
                {
                    list.IsDelete = true;
                }
                var info = Clients.Update(list);
                if (info != null)
                {
                    str.Code = (int)CommonErrorCode.Success;
                    str.Msg = "删除成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return str;
        }
    }
}
